package cn.calm.mvc;

import javax.servlet.ServletContext;

import org.osgi.framework.BundleContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class ExtServlet extends DispatcherServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BundleContext bundleContext;
	private OsgiHandlerMapping bean;
	@Override
	protected void onRefresh(ApplicationContext context) {
		super.onRefresh(context);
		bean = context.getBean(OsgiHandlerMapping.class);
		ServletContext servletContext = getServletContext();
		Object attribute = servletContext.getAttribute("osgi-bundlecontext");
		bundleContext=(BundleContext) attribute;
		bean.setBundleContext(bundleContext);
		bundleContext.addServiceListener(bean);
	}
	@Override
	public void destroy() {
		if(bundleContext!=null && bean!=null){
			bundleContext.removeServiceListener(bean);
		}
		super.destroy();
	}
}
